<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>items</title>
    <script src="js/userOps.js"></script>
    <script>
        function addItem() {
            console.trace("enter addItem");
            var username = document.getElementById("username").value;
            var password = document.getElementById("password").value;
            var ID = document.getElementById("ID").value;
            var ownerID = document.getElementById("ownerID").value;
            var description = document.getElementById("description").value;
            var price = document.getElementById("price").value;
            var images = document.getElementById("images").value;
            var status = document.getElementById("status").value;

            var token = user_login(username, password);
            var itemObj = {
                    "ID": ID,
                    "ownerID": ownerID,
                    "description": description,
                    "price": price,
                    "images": images,
                    "status": status
            };
            var resp = item_Add(token, itemObj);
            document.getElementById("ownerID").value = resp.itemInfo.ownerID;
            document.getElementById("ID").value = resp.itemInfo.ID;
        }
        function getItemList() {
            var username = document.getElementById("username").value;
            var password = document.getElementById("password").value;
            var token = user_login(username, password);
            var resp = item_get_list(token);
            var itemid = parseInt(document.getElementById("ID").value);
            // list all items under user
            document.getElementById("itemList").innerHTML = "";
            for (var i = 0; i < resp.items.length; i++) {
                console.trace(JSON.stringify(resp.items[i]));
                document.getElementById("itemList").innerHTML +=
                        i + ":" + resp.items[i].ID + " ";
                document.getElementById("itemList").innerHTML +=
                        resp.items[i].description;
                document.getElementById("itemList").innerHTML +=
                        "<br>";
            }
            // get info for specific item
            if (isNaN(itemid) || itemid <= 0) {
                return
            }
            document.getElementById("ownerID").value = "";
            document.getElementById("description").value = "";
            document.getElementById("price").value = "";
            document.getElementById("images").value = "";
            document.getElementById("status").value = "";
            for (var it = 0; it < resp.items.length; it++) {
                if (resp.items[it].ID != itemid) {
                    continue;
                }
                document.getElementById("ownerID").value = resp.items[it].ownerID;
                document.getElementById("description").value = resp.items[it].description;
                document.getElementById("price").value = resp.items[it].price;
                document.getElementById("images").value = resp.items[it].images;
                document.getElementById("status").value = resp.items[it].status;
            }
        }
        function thisAddComments() {
            var username = document.getElementById("username").value;
            var password = document.getElementById("password").value;
            var token = user_login(username, password);
            var resp = item_get_list(token);
            var itemid = parseInt(document.getElementById("ID").value);
            var commentText = document.getElementById("comments").value;
            if (isNaN(itemid) || itemid <= 0) {
                return
            }
            var commentObj = {
                "content": commentText,
                "itemID": itemid
            };
            addComments(token, commentObj)
        }
        function thisGetCommnets() {
            var username = document.getElementById("username").value;
            var password = document.getElementById("password").value;
            var token = user_login(username, password);
            var itemid = parseInt(document.getElementById("ID").value);
            var resp = getComments(token, itemid);
            document.getElementById("comment").innerHTML = "";
            var commentTag = document.getElementById("comment");
            for (var i = 0; i < resp.comment.length; i++) {
                commentTag.innerHTML +=
                        resp.comment[i].ID + " " +
                        resp.comment[i].content +
                        "<br>";
            }
        }
    </script>
</head>
<body>
<form>
    <table border="1">
        <tr>
            <td>username</td>
            <td><input type="text" id="username"></td>
        </tr>
        <tr>
            <td>psw</td>
            <td><input type="password" id="password"></td>
        </tr>
        <tr>
            <td>ID</td>
            <td><input type="text" id="ID"></td>
        </tr>
        <tr>
            <td>ownerID</td>
            <td><input type="text" id="ownerID"></td>
        </tr>
        <tr>
            <td>description</td>
            <td><input type="text" id="description"></td>
        </tr>
        <tr>
            <td>price</td>
            <td><input type="text" id="price"></td>
        </tr>
        <tr>
            <td>images</td>
            <td><input type="text" id="images"></td>
        </tr>
        <tr>
            <td>status</td>
            <td><input type="text" id="status"></td>
        </tr>
        <tr>
            <td>Comments of this item</td>
            <td><input type="text" id="comments"></td>
        </tr>
    </table>
    <input type="button" value="addItem" onclick="addItem()">
    <input type="button" value="getItemList" onclick="getItemList()">
    <input type="button" value="addComments" onclick="thisAddComments()">
    <input type="button" value="getComments" onclick="thisGetCommnets()">
    <div id="itemList"></div>
    Comments:<br>
    <div id="comment"></div>
</form>
</body>
</html>